import pygame
from pygame.sprite import Sprite
from ship import Ship


class Alien(Sprite):
    """代表舰队中单个外星人的类"""

    def __init__(self, ai_game):
        """初始化外星人并设置其起始位置。"""
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings

        # 加载外星人图像并设置其rect矩形属性
        self.image = pygame.image.load('../images/alien.png')
        self.rect = self.image.get_rect()

        # 每个新外星人最初在屏幕左上角附近启动
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # 存储外星人的确切水平位置
        self.y = float(self.rect.y)

    def check_edges(self):
        """检查外星人是否撞到屏幕边缘"""
        screen_rect = self.screen.get_rect()
        ship = Ship(self)
        if self.rect.bottom >= screen_rect.bottom or self.rect.top - ship.rect.height <= 0:
            return True

    def update(self):
        """移动外星人"""
        self.y += (self.settings.alien_speed * self.settings.fleet_direction)
        self.rect.y = self.y